/*
 * Copyright (C) 2009 Show SMS open source project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.bydavy.util.showsms;

//~--- non-JDK imports --------------------------------------------------------

import android.app.Activity;
import android.app.AlertDialog;

import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.DialogInterface.OnClickListener;
import android.content.SharedPreferences;

import android.preference.PreferenceManager;

import com.bydavy.android.showsms.R;

/**
 * Class to manage the EULA dialog. It automatically detect if the EULA need to appear or not, and display it or not.
 * Must be call on the begin on the "onCreate" of each application Activity.
 * The class will close the caller Activity if the EULA is refused by the user
 * When EULA is accepted, it will never prompt again the user
 *
 * @version        1.0, 09/06/27
 * @author         Davy L.
 */
public class Eula {
    //~--- methods ------------------------------------------------------------

    /**
     * Must be call on the  "onCreate" of each application Activity.
     *     Will display the EULA only if wasn't yeat accepted.
     *     If EULA is refused, the caller <activity> will be killed.
     *
     * @param activity Caller Activity to kill if the EULA is refused by the user
     */
    public static void show(final Activity activity) {
        SharedPreferences mySharedPreferences =
            PreferenceManager.getDefaultSharedPreferences(activity.getApplicationContext());

        // mySharedPreferences.edit().putBoolean(activity.getString(R.preferences.eula_key), false).commit();
        // loading enabled status from SharedPreferences
        boolean eula_def = Boolean.valueOf(activity.getString(R.preferences.eula_def));
        boolean accepted = mySharedPreferences.getBoolean(activity.getString(R.preferences.eula_key), eula_def);

        if (!accepted) {
            final AlertDialog.Builder builder = new AlertDialog.Builder(activity);

            builder.setTitle(R.string.eula_title);
            builder.setMessage(R.string.eula_msg);
            builder.setPositiveButton(R.string.eula_accept, new OnClickListener() {
                @Override
                public void onClick(DialogInterface arg0, int arg1) {

                    // TODO Auto-generated method stub
                    accept(activity);
                }
            });
            builder.setNegativeButton(R.string.eula_refuse, new OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    refuse(activity);
                }
            });
            builder.setOnCancelListener(new OnCancelListener() {
                @Override
                public void onCancel(DialogInterface dialog) {
                    refuse(activity);
                }
            });
            builder.setCancelable(true);
            builder.show();
        }
    }

    /**
     * Action processed when EULA is refused
     *
     * @param activity caller activity to kill
     */
    private static void refuse(Activity activity) {
        activity.finish();
    }

    /**
     * Action processed when EULA is accepted
     * Save EULA yet readed status
     * @param activity  caller activity
     */
    private static void accept(Activity activity) {
        SharedPreferences mySharedPreferences =
            PreferenceManager.getDefaultSharedPreferences(activity.getApplicationContext());

        mySharedPreferences.edit().putBoolean(activity.getString(R.preferences.eula_key), true).commit();
    }
}
